package app.agent;

import app.env.Data;
import app.env.ShowGui;
import app.env.Position;
import framework.agent.CustomAgent;
import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Name: TargetAgent
 * @Description:
 * @Author: colfish
 * @Date: 2021/4/14 21:45
 * 好好学习，天天向上！
 */
public class TargetAgent extends CustomAgent {
    private static final long serialVersionUID = 8404001603351509479L;

    private Logger logger = LogManager.getLogger(this.getClass());

    // 自己的位置
    private Position position;
    // 散发信号的范围
    private int range;
    // 发送信号的AdminAgent
    private AID adminAID;

    // 行为种类
    private static final String init = "init";
    private static final String detectEmit = "detectEmit";
    private static final String end = "end";


    @Override
    public void setup() {
        super.setup();

        // 将自身存入Data
        addBehaviour(new Init(this));

        logger.info(this.getAID().getLocalName() + " Start running!");

    }

    public class Init extends OneShotBehaviour{
        private static final long serialVersionUID = -4354331778064731669L;
        private final TargetAgent agent;

        public Init(TargetAgent agent){
            this.agent = agent;
        }

        @Override
        public void action() {
            Data.targetAgents.add(agent);
            agent.initSignal();
            logger.info("TargetAgent: <" + agent.getLocalName() + "> Init Finished...");
        }
    }

    /**
     * 初始化目标代理，并在地图上的range范围内发送信号。
     */
    public void initSignal(){
        // 根据参数获取位置和影响范围
        this.position = (Position) getArguments()[0];
        this.range = (int)getArguments()[1];

        int x = position.x;
        int y = position.y;
        ShowGui.board[x][y] = Data.TARGET_SIGNAL;
        for(int m = x-range; m <= x+range; m++){
            for(int n = y-range; n <= y+range; n++){
                if(m==x&&n==y)
                    continue;
                if(ShowGui.board[m][n] < 0 || ShowGui.board[m][n]>2000)
                    continue;
                Position p2 = new Position(m, n);
                // targrt目标点附近，距离小于target的点有信号
                double dis = position.getDistance(p2);
                if(dis <= range){
                    // 将与目标点的距离转换为对应的信号值0~range ==> MAX_SIGN_NUM~0
                    ShowGui.board[m][n] = (int) Math.round((range-dis)/range* ShowGui.MAX_SIGN_NUM);
                }
            }
        }
    }

    /**
     * 目标代理停止向周围发送信号，并将自身所在位置的信号值改为-100002
     */
    public void clearSignal(){
        int x = position.x;
        int y = position.y;
        ShowGui.board[x][y] = Data.TARGET_STOP_SIGNAL;
        for(int m = x-range; m < x+range; m++){
            for(int n = y-range; n < y+range; n++){
                if(m==x&&n==y)
                    continue;
                Position p2 = new Position(m, n);
                // target目标点附近，距离小于target的点有信号
                double dis = position.getDistance(p2);
                if(dis < range && ShowGui.board[m][n] > 0 && ShowGui.board[m][n] < ShowGui.MAX_SIGN_NUM){
                    // 将目标点周围的0~2000的信号值清零
                    ShowGui.board[m][n] = 0;
                }
            }
        }
    }

    public Position getPosition() {
        return position;
    }

    public int getRange() {
        return range;
    }

    public AID getAdminAID() {
        return adminAID;
    }

    public void setAdminAID(AID adminAID) {
        this.adminAID = adminAID;
    }
}
